package com.flexrrb.controls
{
	import flash.events.Event;
	import flash.events.MouseEvent;

	import mx.controls.Button;
	import mx.controls.LinkButton;
	import mx.controls.TextInput;
	import mx.core.EdgeMetrics;

	[Event(name="iconButtonClick", type="flash.events.Event")]

	[Style(name="iconButtonIcon", type="Class", inherit="no")]

	public class SuperTextInput extends TextInput
	{

		public function SuperTextInput()
		{
			super();
		}
		protected var _maskString:String;
		protected var _iconButton:LinkButton;
		protected var _iconButtonLabel:String;
		protected var _iconButtonToolTip:String;
		protected var _iconButtonIcon:Class;
		protected var _iconButtonWidth:Number=25;
		protected var _iconButtonClickable:Boolean=true;
		protected var _iconButtonRequired:Boolean=false;
		protected var _iconButtonPlacement:String="right";

		public function get maskString():String
		{
			return _maskString;
		}

		public function set maskString(value:String):void
		{
			_maskString=value;
		}

		public function get iconButton():Button
		{
			return _iconButton;
		}

		public function get iconButtonLabel():String
		{
			return _iconButtonLabel;
		}

		public function set iconButtonLabel(value:String):void
		{
			_iconButtonLabel=value;
		}

		public function get iconButtonIcon():Class
		{
			return _iconButtonIcon;
		}

		public function set iconButtonIcon(value:Class):void
		{
			_iconButtonIcon=value;
		}

		[Inspectable(enumeration="right,left", defaultValue="right")]

		public function get iconButtonPlacement():String
		{
			return _iconButtonPlacement;
		}

		public function set iconButtonPlacement(value:String):void
		{
			_iconButtonPlacement=value;
			invalidateDisplayList();
		}

		[Inspectable(enumeration="false,true", defaultValue="true")]

		public function get iconButtonClickable():Boolean
		{
			return _iconButtonClickable;
		}

		public function set iconButtonClickable(value:Boolean):void
		{
			_iconButtonClickable=value;
			invalidateDisplayList();
		}

		[Inspectable(enumeration="false,true", defaultValue="false")]

		public function get iconButtonRequired():Boolean
		{
			return _iconButtonRequired;
		}

		public function set iconButtonRequired(value:Boolean):void
		{
			_iconButtonRequired=value;
			invalidateDisplayList();
		}

		public function get iconButtonToolTip():String
		{
			return _iconButtonToolTip;
		}

		public function set iconButtonToolTip(value:String):void
		{
			_iconButtonToolTip=value;
			invalidateDisplayList();
		}

		public function get iconButtonWidth():Number
		{
			return _iconButtonWidth;
		}

		public function set iconButtonWidth(value:Number):void
		{
			_iconButtonWidth=value;
			invalidateDisplayList();
		}

		override protected function createChildren():void
		{
			super.createChildren();
			if (!_iconButton && iconButtonRequired)
			{
				createIconButton();
			}
		}

		override protected function measure():void
		{
			super.measure();
		}

		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth, unscaledHeight);

			if (!_iconButton && iconButtonRequired)
			{
				createIconButton();
			}
			if(_iconButton && !iconButtonRequired){
				removeIconButton();
			}
			if (iconButtonRequired)
			{
				var bm:EdgeMetrics;

				bm=EdgeMetrics.EMPTY;
				textField.width-=_iconButtonWidth;
				_iconButton.width=iconButtonWidth;
				_iconButton.height=this.height - 2;
				_iconButton.y=bm.top + (this.height - _iconButton.height) / 2;
				if (_iconButtonPlacement == "right")
				{
					_iconButton.x=this.width - _iconButton.width - 1;
				}
				else
				{
					textField.x=_iconButton.width;
					_iconButton.x=1;
				}
				if (!iconButtonClickable)
				{
					_iconButton.buttonMode=false;
					_iconButton.enabled=false;
				}
				else
				{
					_iconButton.addEventListener(MouseEvent.CLICK, iconButtonClickEvent);
				}
				_iconButton.toolTip=_iconButtonToolTip;
			}
		}

		private function createIconButton():void
		{
			_iconButton=new LinkButton();
			_iconButton.label=_iconButtonLabel;
			_iconButton.setStyle("cornerRadius", 0);
			if (_iconButtonIcon)
			{
				_iconButton.setStyle("icon", _iconButtonIcon);
			}
			addChild(_iconButton);
		}
		
		private function removeIconButton():void{
			_iconButton.removeEventListener(MouseEvent.CLICK, iconButtonClickEvent);
			removeChild(_iconButton);
			_iconButton = null;
		}

		private function iconButtonClickEvent(event:MouseEvent):void
		{
			//event.type = "iconButtonClick";
			dispatchEvent(new Event("iconButtonClick"));
		}
	}
}